package com.sailfish.springbootdemo.dao.db4;

import com.sailfish.springbootdemo.common.DB4TableName;
import com.sailfish.springbootdemo.pojo.db4.LoginRecord;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface LoginRecordDao {

    /**
     * 查询所有
     *
     * @return
     */
    @Results(id = "loginRecordResultMap", value = {
            @Result(column = "login_record_id", property = "loginRecordId", jdbcType = JdbcType.INTEGER),
            @Result(column = "user_id", property = "userId", jdbcType = JdbcType.INTEGER),
            @Result(column = "ip_address", property = "ipAddress", jdbcType = JdbcType.VARCHAR),
            @Result(column = "network_seg", property = "networkSeg", jdbcType = JdbcType.VARCHAR),
            @Result(column = "login_result", property = "loginResult", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fail_reason", property = "failReason", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
    })
    @Select("<script>" +
            "select * from " + DB4TableName.LOGIN_RECORD + " t1 where 1 = 1 " +
            "<if test='loginResult != null and loginResult != \"\" '>" +
            " and t1.login_result = #{loginResult}" +
            "</if>" +
            " order by t1.create_time desc" +
            "<if test='begin != null and pageSize != null and pageSize != \"\" '>" +
            " limit #{begin},#{pageSize}" +
            "</if>" +
            "</script>")
    List<LoginRecord> getAllLoginRecord(@Param("loginResult") String loginResult, @Param("begin") Integer begin, @Param("pageSize") Integer pageSize);

    /**
     * 新增
     *
     * @param loginRecord
     * @return
     */
    @Insert("insert into " + DB4TableName.LOGIN_RECORD + "(user_id,ip_address,network_seg,login_result,fail_reason,create_time) values " +
            "(#{loginRecord.userId},#{loginRecord.ipAddress},#{loginRecord.networkSeg},#{loginRecord.loginResult},#{loginRecord.failReason},#{loginRecord.createTime})")
    Integer addLoginRecord(@Param("loginRecord") LoginRecord loginRecord);
}
